{#
Product macros is splitted to multiple files and are extended together.

Inheritance order for product macors:
    product_image.jinja
    product_information.jinja
    product_ordering.jinja
    theme/product.jinja
#}
{% extends "shuup/front/macros/product_image.jinja" %}
{% from "shuup/front/macros/general.jinja" import render_supplier_info, render_availability_info %}

{% macro product_box(
        product, show_image=True, show_description=True, class="product-box",
        thumbnail_size=(500,500), supplier=None) %}
    {% set shop_product = product.get_shop_instance(request.shop, allow_cache=True) %}
    {% if supplier %}
        {% set u = url("shuup:supplier-product", supplier_pk=supplier.pk, pk=product.pk, slug=product.slug) %}
    {% else %}
        {% set u = url("shuup:product", pk=product.pk, slug=product.slug) %}
        {% set supplier = shop_product.get_supplier() %}
    {% endif %}

    {% set show_supplier_info = (supplier and xtheme.get("show_supplier_info")) %}
    <div class="product-card {{ class }}" id="product-{{ product.id }}">
        <a href="{{ u }}" rel="product-detail" title="{{ product.name }}">
            {% if product|is_discounted(supplier=supplier) %}
                <span class="badge product-badge sale">{{ -(product|discount_rate(supplier=supplier))|percent }}</span>
            {% endif %}
            {% if show_image %}
                {% set product_image = shop_product.public_primary_image or shop_product.public_images.first() %}
                {% set primary_image_thumb = product_image|thumbnail(size=thumbnail_size) %}
                <div class="image-wrap">
                    {% if product_image and primary_image_thumb %}
                        {% set image_url = primary_image_thumb %}
                    {% else %}
                        {% set image_url = STATIC_URL ~ "shuup/front/img/no_image.png" %}
                    {% endif %}
                    <div class="image" style="background-image:url('{{ image_url }}')"></div>
                </div>
            {% endif %}
            <div class="detail-wrap">
                <div class="name">
                    {{ product.name }}
                </div>
                {% if show_supplier_info %}{{ render_supplier_info(supplier, render_link=False) }}{% endif %}
                {% if shop_product and shop_product.available_until %}{{ render_availability_info(shop_product) }}{% endif %}
                <div class="actions-wrap">
                    {% if show_prices() and not xtheme.get("hide_prices") %}
                    <div class="price">
                        {{ render_product_price(product, show_units=False, supplier=supplier) }}
                    </div>
                    {% endif %}
                </div>
                {% if show_description and product.short_description %}
                    <p class="description">{{ product.short_description|safe_product_description }}</p>
                {% endif %}
            </div>
        </a>
        <div class="actions clearfix">
            <button type="button"
                    class="btn btn-block text-muted"
                    onclick="showPreview('{{ product.id }}');">
                <i class="fa fa-eye"></i> {% trans %}Preview{% endtrans %}
            </button>
        </div>
    </div>
{% endmacro %}

{% macro render_product_price(product, show_units=True, supplier=None) %}
    {% if show_prices() and not xtheme.get("hide_prices") %}
        {%- set unit = product.get_shop_instance(request.shop, allow_cache=True).unit -%}
        {%- set (per_qty, per_text) = unit.get_per_values(show_units) -%}
        <div class="price-line">
            <span class="lead">
                {%- if product.is_variation_parent() -%}
                    {% set (min_price, max_price) = product|price_range(per_qty, supplier=supplier) %}
                    {% if min_price == max_price %}
                        {% if product|is_discounted(supplier=supplier) -%}
                        <small class="text-muted original-price original"><s>
                            {{- product|base_price(per_qty, supplier=supplier) -}}
                        </s></small>
                        {%- endif %}
                        <strong>{{ min_price }}</strong>
                    {% else %}
                        <strong>{{ min_price }} &ndash; {{ max_price }}</strong>
                    {% endif %}
                {%- else -%}
                    {% if product|is_discounted(supplier=supplier) -%}
                        <small class="text-muted original-price original"><s>
                            {{- product|base_price(per_qty, supplier=supplier) -}}
                        </s></small>
                    {%- endif %}
                    <strong>{{ product|price(per_qty, supplier=supplier) }}</strong>
                {%- endif -%}
                {%- if per_text -%}
                    <small> / {{ per_text }}</small>
                {%- endif -%}
            </span>
        </div>
    {% endif %}
{% endmacro %}

{% macro render_product_detail_top_placeholder() %}
    {% placeholder "detail_top_placeholder" %}{% endplaceholder %}
{% endmacro %}

{% macro render_product_placeholder_section() %}
    {% placeholder "product_bottom" %}
        {% row %}
            {% plugin "product_cross_sells" %}
                type = "related"
                title = "Related Products"
            {% endplugin %}
        {% endrow %}
        {% row %}
            {% plugin "product_cross_sells" %}
                type = "recommended"
                title = "We recommend these products"
            {% endplugin %}
        {% endrow %}
        {% row %}
            {% plugin "product_cross_sells" %}
                type = "computed"
                title = "Users who bought this product also bought"
            {% endplugin %}
        {% endrow %}
    {% endplaceholder %}
{% endmacro %}

{% macro render_also_available_products(product) %}
    {% if package_parents %}
        <hr>
        <h3>{% trans %}Also available in{% endtrans %}</h3>
        <div class="row">
            {% for parent in package_parents %}
                <div class="col-md-3">
                    {{ product_box(parent) }}
                </div>
            {% endfor %}
        </div>
    {% endif %}
{% endmacro %}

{% macro render_product_detail_ready(product) %}
    <script>
        $(function() {
            updatePrice("{{ product.id }}");
        });
    </script>
{% endmacro %}

{% macro render_extrajs() %}
    {{ render_product_image_js() }}
    {{ render_product_detail_ready(product) }}
{% endmacro %}
